var picsrc = "";
var videoid="";
$(document).ready(function(){
	//$('#name').text(sessionStorage.getItem("name"));
	getshop();
	gettoday();
	layui.define(['upload'], function(exports){
	var upload = layui.upload;
			upload.render({
				elem: '#videoUploadBtn'
				,url: layui.setter.host+'/upload/videoOss'
				,accept: 'video'
				,size:1024*100
				,multiple: true
		  		,before: function(obj){
					console.log(videoid);
					if(videoid ==""){
						layer.msg('视频上传中，请稍后', {  
							  icon: 16
							  ,shade: 0.01
							});
					}else{
						alert("今日视频已上传");
						throw 'error'; //阻止文件上传
						return false;
					}	
				}
				,done: function(res){
					picsrc = res.src;
					videoSuoShow(picsrc);
					layer.msg(res.msg);
					var val = $(this.item).prev("div").children("input").val();
					val = val==''?'':val+',';
					$(this.item).prev("div").children("input").val(val+res.src)
					gettoday1();
				},error: function(index, upload){
					console.log(upload)
//					alert(JSON.stringify(upload))
				    //当上传失败时，你可以生成一个“重新上传”的按钮，点击该按钮时，执行 upload() 方法即可实现重新上传
				  }
			});
	});
	//设置视频缩略图
	function videoSuoShow(videoStr){
		var videoArray = videoStr.split(',');
		for (var i = 0; i < videoArray.length; i++) {
			if(videoArray[i] != ''){
				$('#videodiv').append(
					'<div class="imgbox add_pos_re"  value="'+ videoArray[i] +'">'+
					'<img class="layui-upload-img" src="../genius/layui/images/video1.png">'+	
					'<div class="closebox add_pos_ab ab_t0r0" onclick="deleteImg(this)"><img src="../genius/layui/images/delete.png"></div></div>'	
				);
			}
		}
		savevideo();
	}
function getshop(){
	if(shopcode == '0' || shopcode == 0){
		$('#shopname').text("所有门店        ");
	}else{
		$.ajax({  
			url: "../../unite_platform/mobile_page/get_shop"
			,type:'post'
			,async: false
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({"shopcode":shopcode}) 
			,dataType:'json'
			,success: function(res){
				$('#shopname').text(res.data[0].deptcode+res.data[0].deptname);
			}
		});
	}
}
function savevideo(){
		$.ajax({  
			url: "../../unite_platform/mobile_page/save_video"
			,type:'post'
			,async: false
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({"shopcode":shopcode,"video":picsrc,"lrr":sessionStorage.getItem("userId")}) 
			,dataType:'json'
			,success: function(res){
				console.log(res);
			}
		});
	}
});	
function gettoday(){
		$.ajax({  
			url: "../../unite_platform/mobile_page/get_today"
			,type:'post'
			,async: false
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({"shopcode":shopcode}) 
			,dataType:'json'
			,success: function(res){
				if(res.data.length>0){
						videoid =res.data[0].id;
						$('#videodiv').append(
						'<div class="imgbox add_pos_re"  value="'+res.data[0].video+'">'+
						'<img class="layui-upload-img" src="../genius/layui/images/video1.png">'+	
						'<div class="closebox add_pos_ab ab_t0r0" onclick="deleteImg(this)"><img src="../genius/layui/images/delete.png"></div></div>'	
					);
				}
			
			}
		});
	}

function gettoday1(){
		$.ajax({  
			url: "../../unite_platform/mobile_page/get_today"
			,type:'post'
			,async: false
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({"shopcode":shopcode}) 
			,dataType:'json'
			,success: function(res){
				if(res.data.length>0){
						videoid =res.data[0].id;
				}
			
			}
		});
	}
//获取删除oss文件的Key
function getKey(url) {
//	url = "http://youxian-shoutu.oss-cn-zhangjiakou.aliyuncs.com/1544850245369.png?Expires=1860210239&OSSAccessKeyId=LTAI67zMng4CyPb0&Signature=zuSxvJO1%2BsTbTBb8x1B4VwG5VWw%3D";
    var str = url.split("?")[0].substring(7);
    var ss = str.indexOf("/");
    return decodeURI(str.substring(ss + 1, str.length));
}

//删除Oss文件
function deleteImg(obj) {
		gettoday1();
    layer.msg('真的要删除吗？', {
        time: 0 //不自动关闭
        , btn: ['删除', '取消']
        , yes: function (index) {
            layer.close(index);
            //获取图片地址
            var imgUrl = $(obj).parent().parent().find('.imgbox').attr('value');
            var key = getKey(imgUrl);
			$(obj).parent().remove();
            $.ajax({
                url: '../../unite_platform/upload/ImgOssDelete'
                , type: 'post'
                , dataType: 'json'
                , contentType: "application/json; charset=utf-8"
                , data: JSON.stringify({'key': key})
                , done: function (res) {
                    //请求成功后
                    if (res.code == '0') {
                        //获取值的id
                        var inputId = $(obj).parent().parent().parent().find('input').attr('id');
                        //删除图片显示
                        $(obj).parent().remove();
                        var imgStr = '';
                        $('#' + inputId).parent().nextAll("div[imgshow]").find('.layui-upload-img').each(function (i, el) {
                            imgStr += $(el).attr('src') + ',';
                        });
                        if (imgStr.length >= 1) {
                            imgStr = imgStr.substring(0, imgStr.length - 1)
                        }
                        $('#' + inputId).val(imgStr);
                        imgStr = null;
                    }
                    layer.msg(res.msg);
                }
            });
        }
    });

	$.ajax({  
		url: "../../unite_platform/mobile_page/del_video"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"delData":[{"id":videoid}]}) 
		,dataType:'json'
		,success: function(res){
			videoid = "";
			/*$('#videodiv').append(
					'<video style="width:100%;height:100%" src="'+res.data[0].video+'" controls="controls"></video>'
			);*/
		}
	});
}
